package com.keke.beauty.auth.activityInvolvement.controller;

import com.keke.beauty.auth.activityInvolvement.entity.TabActivityInvolvement;
import com.keke.beauty.auth.activityInvolvement.mapper.TabActivityInvolvementMapper;
import com.keke.beauty.auth.activityInvolvement.mapper.TabIntegralsFlowMapper;
import com.keke.beauty.auth.activityInvolvement.service.impl.TabActivityInvolvementServiceImpl;
import com.keke.beauty.utils.exception.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.keke.beauty.auth.activityInvolvement.service.TabActivityInvolvementService;
import com.keke.beauty.utils.pojo.Result;
import com.keke.beauty.utils.tools.ResultUtil;

/**
 * 活动参与控制层
 *
 * @author
 * @Data 2019年02月27日 11:06:36
 */
@RestController
@RequestMapping("wx/activityInvolvement")
public class WxActivityInvolvementController {

    @Autowired
    private TabActivityInvolvementService service;

    @Autowired
    private TabActivityInvolvementMapper mapper;

    @Autowired
    TabActivityInvolvementServiceImpl tabActivityInvolvementService;

    /**
     * 已报名列表
     *
     * @param tableInfo
     * @return
     * @throws Exception
     * @author
     * @Data 2019年02月27日 11:06:36
     * @Date 2018年1月15日 下午2:29:08
     */
    @GetMapping("singUpList/{id}")
    public Result getSingUpList(@PathVariable("id") Integer id) {
        return ResultUtil.success(service.findSingUpList(id));
    }

    /**
     * 功能描述：活动报名
     * 创建时间：2019年2月27日 下午3:57:38
     *
     * @return
     */
    @PostMapping("signUp")
    public Result signUp(String name, String phone, String openId, Integer activityId) {
        service.signUp(name, phone, openId, activityId);
        return ResultUtil.success();
    }

    /**
     * 功能描述：活动点赞
     * 创建时间：2019年2月27日 下午2:33:46
     *
     * @param openId          当前用户openId
     * @param activityId      活动id
     * @param referralsOpenId 推荐人openId
     * @return
     */
    @PostMapping("salute")
    public Result salute(@RequestParam String openId, @RequestParam int activityId, String referralsOpenId, String username, String phone) {
        service.salute(openId, activityId, referralsOpenId, username, phone);
        return ResultUtil.success();
    }

    /**
     * 记录活动分享
     *
     * @param openId
     * @param activityId
     * @param referralsOpenId
     */
    @PostMapping("activityShare")
    public Result activityShare(@RequestParam String openId, @RequestParam int activityId, @RequestParam String referralsOpenId) {
        tabActivityInvolvementService.addIntegrals(openId, activityId, 1, null, null, referralsOpenId);
        return ResultUtil.success();
    }

    /**
     * 检测报名状态
     *
     * @param openId
     * @param activityId
     * @return
     */
    @PostMapping("checkSalute")
    public Result checkSalute(@RequestParam String openId, @RequestParam int activityId) {
        TabActivityInvolvement param = new TabActivityInvolvement();
        param.setOpenid(openId);
        param.setActivityId(activityId);
        TabActivityInvolvement data = mapper.selectByOpenId(param);
        if (StringUtils.isEmpty(data)) {
            throw new RRException("你是非法用户！");
        }
        if (data.getIsSignUp() == 1) {
            throw new RRException("重复报名");
        }
        return ResultUtil.success();
    }
}